layui.use(['table', 'admin', 'ax', 'jquery', 'layer', 'form', 'upload', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var laydate = layui.laydate;

    form.verify({
        double: function (value) {
            var patt2=/^(\-|\+)?\d+(\.\d+)?$/;
            if (patt2.test(value) == false) {
                return '你输入的不是数字';
            }
            // if (/^\d+$/.test(value) == false && /^\d+\.\d+$/.test(value) == false) {
            //     return '你输入的不是数字';
            // }
        }
    });

    /**
     * bty_zx_demo管理
     */
    var ZxChengben = {
        tableId: "zxChengbenTable"
    };

    /**
     * 初始化表格的列
     */
    ZxChengben.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'btyZxChengbenId', hide: true, title: '主键'},
            {
                field: 'riqi', width: 110, sort: true, title: '日期', templet: function (data) {
                    return layui.util.toDateString(data.riqi, "yyyy-MM-dd");
                }
            },
            {field: 'brandIdView', sort: true, title: '门店', width: 230, align: "center"},
            // {field: 'btyZxHuopinIdView', sort: true, title: '科目', align: "center"},
            {field: 'zhaiyao', sort: true, title: '摘要', width: 200, align: "center"},
            // {field: 'btyyBxdaId', sort: true, title: '厂家'},
            {field: 'btyyBxdaIdView', width: 100, sort: true, title: '报销人', align: "center"},
            {field: 'zongjia', width: 100, sort: true, title: '金额', align: "center", totalRow: true},

            //     templet: function (d) {
            //     return changjiaFun(d);
            // }
            // {field: 'btyZxHuopinId', sort: true, title: '货品'},
            // {field: 'danwei', sort: true, title: '单位',edit: 'text',align:"center"},
            // {field: 'chicun', sort: true, title: '尺寸',edit: 'text',align:"center"},
            // {field: 'shuliang', sort: true, title: '数量',edit: 'text',align:"center"},
            // {field: 'jiage', sort: true, title: '单价',edit: 'text',align:"center"},

            {field: 'yhKahao', sort: true, title: '银行卡号', width: 180, align: "center"},
            {field: 'yhKaihuhang', sort: true, title: '开户行', width: 180, align: "center"},
            {field:'status',sort: true,title: "打款状态",width: 100,templet:function(data){
                    switch (data.status) {
                        case "1":return "已打款";break;
                        default :return "未打款";break;
                    }
                }},
            {field: 'beizhu', sort: true, title: '备注', align: "center"},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 230}
        ]];
    };


    // var languageAjax8 = new $ax(Feng.ctxPath + "/btyBxda/getAllVendorList");
    // var languageResult8 = languageAjax8.start();
    // console.info(languageResult8);
    // var selTouBu8 = '';
    // $.each(languageResult8.data, function (index, val) {
    //     selTouBu8 = selTouBu8 + '<option value=' + val.yhk + '>' + val.xm + '</option>';
    // });
    // var selWeiBu8 =selTouBu8+ '</select>';
    //
    // function changjiaFun(d) {
    //     var selTouBu = '<select  name="btyyBxdaIdViw" class="sel_xlk" lay-filter="stateSelect" lay-verify="required" data-state="' + d. selected + '" data-value="' + d.btyyBxdaId + '" >';
    //     return selTouBu + selWeiBu8;
    // }
    /**
     * 初始门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/mdzx/mdzxList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        if(val.btyBrandIdView!=undefined){
            var pinyin = ConvertPinyin(val.btyBrandIdView);
            //添加的门店
            $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView +"-"+ pinyin + "</option>");
            //查询的门店
            // $("#mdzxSearch").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "</option>");

            //导出时选择的门店
            // $("#btyZxMdzxId1").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "</option>");
            $("#btyZxMdzxId1").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView +"-"+ pinyin + "</option>");
        }
    });
    form.render('select');







    /**
     * 点击查询按钮
     */
    ZxChengben.search = function () {
        var queryData = {};
        queryData['btyZxMdzxId'] = $("#btyZxMdzxId1").val();
        queryData['riqi'] = $("#riqi1").val();
        queryData['zhaiyao'] = $("#zhaiyao_q").val();
        queryData['beizhu'] = $("#beizhu_q").val();
        table.reload(ZxChengben.tableId, {where: queryData});
    };

    //监听单元格编辑
    table.on('edit(zxChengbenTable)', function (obj) {
        var value = obj.value, //得到修改后的值
            data = obj.data, //得到所在行所有键值
            field = obj.field; //得到字段
        // shuliang =obj.data.shuliang;
        // jiage =obj.data.jiage;
        // total = shuliang*jiage;
        zongjia = obj.data.zongjia;


        data.zongjia = zongjia;
        $.ajax({
            type: "PUT",
            url: "/zxChengben/editItem",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(ZxChengben.tableId);
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) {
            }
        });
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/zxChengben/addItem", function (data) {
            if (data.code != 200) {
                Feng.error("添加失败!" + data.message + "!");
                return;
            }
            Feng.success("添加成功！");
            //清空值
            // $("#btyZxHuopinId").val("");
            // $("#btyyBxdaId").val("");
            // $("#btyZxMdzxId").val("");
            //
            // form.render('select');
            // $("#danwei").val("");
            // $("#chicun").val("");
            // $("#shuliang").val("");
            // $("#jiage").val("");
            // $("#riqi").val("");
            // $("#zhaiyao").val("");
            // $("#yhKahao").val("");
            // $("#yhKaihuhang").val("");
            // $("#daKuan").val("");
            $("#beizhu").val("");
            $("#zongjia").val("");
            // $("#btyyBxdaIdView").val("");
            // $("#yhKahao").val("");
            // $("#yhKaihuhang").val("");
            //刷新列表
            var queryData = {};
            // queryData['riqi'] = $("#riqi").val();
            queryData['riqi'] = $("#riqi").val();
            table.reload(ZxChengben.tableId, {where: queryData});
            table.reload(ZxChengben.tableId);
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });

        //厂家名称
        // ajax.set("btyyBxdaIdView", $("#btyyBxdaId").find("option:selected").text());
        //货品
        var hpname = $("#btyZxHuopinId").find("option:selected").text();
        ajax.set("btyZxHuopinIdView", hpname.split("-")[0]);


        ajax.set(data.field);
        ajax.start();
        return false;
    });

    /**
     * 导出excel按钮
     */
    ZxChengben.exportsExcel = function () {
        //获取导出Excel的数据
        var btyZxMdzxId = $("#btyZxMdzxId1").val();
        var riqi = $("#riqi1").val();
        var url = Feng.ctxPath + "/zxChengben/listExport?btyZxMdzxId=" + btyZxMdzxId+"&riqi="+riqi;
        location.href = url;
    };


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ZxChengben.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/zxChengben/edit?btyZxChengbenId=' + data.btyZxChengbenId + "&btyZxMdzxId=" + $("#btyZxMdzxId1").val();
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ZxChengben.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxChengben/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ZxChengben.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyZxChengbenId", data.btyZxChengbenId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ZxChengben.tableId,
        url: Feng.ctxPath + '/zxChengben/list_dk',
        page: true,
        height: "full-95",
        cellMinWidth: 100,
        totalRow: true,
        limit: 50,
        cols: ZxChengben.initColumn(),
    });
    // done: function (res, curr, count) {
    //     //设置下拉框样式在表格之上 不会遮挡下拉框
    //     $(".layui-table-body").css('overflow', 'visible');
    //     $(".layui-table-box").css('overflow', 'visible');
    //     $(".layui-table-view").css('overflow', 'visible');
    //
    //     var tableElem = this.elem.next('.layui-table-view');
    //     count || tableElem.find('.layui-table-header').css('overflow', 'auto');
    //     layui.each(tableElem.find('select[name="btyyBxdaIdViw"]'), function (index, item) {
    //         var elem = $(item);
    //         elem.val(elem.data('state')).parents('div.layui-table-cell').css('overflow', 'visible');
    //     });
    //     form.render();//刷新表单
    // }
    var para = "";
    if (Feng.getUrlParam("btyZxMdzxId") != null) {
        // para="btyZxMdzxId="+Feng.getUrlParam("btyZxMdzxId");
        $("#btyZxMdzxId1").val(Feng.getUrlParam("btyZxMdzxId"));
        ZxChengben.search();
    }

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZxChengben.search();
    });
    $('#btnQingkong').click(function () {
        $("#btyZxHuopinId").val("");
        $("#btyyBxdaId").val("");
        $("#btyZxMdzxId").val("");

        form.render('select');
        $("#danwei").val("");
        $("#chicun").val("");
        $("#shuliang").val("");
        $("#jiage").val("");
        // $("#riqi").val("");
        $("#zhaiyao").val("");
        $("#yhKahao").val("");
        $("#yhKaihuhang").val("");
        $("#daKuan").val("");
        $("#beizhu").val("");
        $("#zongjia").val("");
        $("#btyyBxdaIdView").val("");
        $("#yhKahao").val("");
        $("#yhKaihuhang").val("");
    });
    // 刷新页面
    $('#refresh').click(function () {
        window.location.href = Feng.ctxPath + '/zxChengben/zx_dakuan';
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ZxChengben.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ZxChengben.exportsExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ZxChengben.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dakuan_queren') {
            ZxChengben.dakuan_queren(data);
        }else if(layEvent === 'dakuan_quxiao') {
            ZxChengben.dakuan_quxiao(data);
        }
    });
    

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/mdzx'
    });

    /**
     * 初始化厂家
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getAllVendorList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        var pinyin = ConvertPinyin(val.xm);

        $("#btyyBxdaId").append("<option value='" + val.yhk + "'>" + val.xm +"-"+ pinyin + "</option>");
    });
    form.render('select');


    // form.on('select(queryhp)',function (data) {
    //     $("#btyZxHuopinId").html("");
    //     $("#btyZxHuopinId").append("<option value=''>全部</option>");
    //     var hpajax = new $ax(Feng.ctxPath + "/zxHuopin/listHuopinSelect");
    //     //hpajax.set("mdzxid",data.value);
    //     var hpajaxResult = hpajax.start();
    //     $.each(hpajaxResult.data, function (index, val) {
    //         $("#btyZxHuopinId").append("<option value='" + val.id + "'>" + val.name + "</option>");
    //     });
    //     form.render('select');
    // });

    //初始货品
    var languageAjax = new $ax(Feng.ctxPath + "/zxHuopin/listHuopinSelect");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        var pinyin = ConvertPinyin(val.name);

        $("#btyZxHuopinId").append("<option value='" + val.btyZxHuopinId + "'>" + val.name +"-"+ pinyin + "</option>");
    });
    form.render('select');


    $("#shuliang").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var jiage = $("#jiage").val();
        var total = shuliang * jiage;
        $("#zongjia").val(total.toFixed(2));
    });
    $("#jiage").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var jiage = $("#jiage").val();
        var total = shuliang * jiage;
        $("#zongjia").val(total.toFixed(2));
    });


    //成本导入
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/zxChengben/importBaoXiaoDan'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            //this.data = {'dataTime': $('#dataTime').val()};//关键代码
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (data) {
            if (data.code == 200) {
                Feng.success("信息导入成功");
                // window.location.reload();
                layer.close(loadIndex);
                table.reload(ZxChengben.tableId);
            } else {
                Feng.error(data.message);
                layer.close(loadIndex);
            }

        }
    });


    laydate.render({
        elem: '#riqi',
        value:Feng.currentDate(),
        max: Feng.currentDate()
    });
    laydate.render({
        elem: '#riqi1',
        format: 'yyyy-MM-dd',
        value:Feng.currentDate(),
        max: Feng.currentDate()
    });

    //监听单元格编辑
    table.on('edit(zxChengbenTable)', function(obj){
        var value = obj.value, //得到修改后的值
            data = obj.data, //得到所在行所有键值
            field = obj.field; //得到字段
        $.ajax({
            type: "PUT",
            url: "/zxChengben/editItem",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(ZxChengben.tableId);

            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) { }
        });
    });

    //选择厂家时，1.将厂家对应的名称赋值给厂家名称 2.将厂家的id(即银行卡号，赋值给银行卡号)
    form.on('select(btyyBxdaId)',function (data) {
        var btyyBxdaIdView= $("#btyyBxdaId").find("option:selected").text();
        var btyyBxdaIdViewarr = btyyBxdaIdView.split("-");

        $("#btyyBxdaIdView").val("");
        if(btyyBxdaIdView!="请选择") {

            $("#btyyBxdaIdView").val(btyyBxdaIdViewarr[0]);
        }
        var btyyBxdaId= $("#btyyBxdaId").val();
        $("#yhKahao").val("");
        if(btyyBxdaId!=""){
            $("#yhKahao").val(btyyBxdaId);
        }
        // $("#btyyBxdaId").parent().find("input").val(btyyBxdaIdViewarr[0]);

    });

    //选择货品时，将货品名称赋值给摘要
    form.on('select(btyZxHuopinId)',function (data) {
        var btyZxHuopinIdView= $("#btyZxHuopinId").find("option:selected").text();
        var btyZxHuopinIdView_str = btyZxHuopinIdView.split("-");
        $("#btyZxHuopinIdView").val("");
        if(btyZxHuopinIdView!="请选择") {
            $("#zhaiyao").val(btyZxHuopinIdView_str[0]);
        }

    });

    form.on('button(qingkong)',function (data) {
        alert("qingkong");

    });

    $("#btyZxMdzxId_div input").on("input",function(e){
        //获取input输入的值
        console.log(e.delegateTarget.value);
    });


    /**
     * 明细
     */
    ZxChengben.detail = function (data) {
        window.location.href = Feng.ctxPath + '/zxChengbenMingxi?btyZxChengbenId=' + data.btyZxChengbenId;
    };
    //打款确认
    ZxChengben.dakuan_queren=function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxChengben/dakuan_queren", function (data) {
                Feng.success("打款确认成功!");
                table.reload(ZxChengben.tableId);
            }, function (data) {
                Feng.error("打款失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyZxChengbenId", data.btyZxChengbenId);
            ajax.start();
        };
        Feng.confirm("是否打款确认?", operation);
    }
    //取消
    ZxChengben.dakuan_quxiao=function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/zxChengben/dakuan_quxiao", function (data) {
                Feng.success("取消成功!");
                table.reload(ZxChengben.tableId);
            }, function (data) {
                Feng.error("取消失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyZxChengbenId", data.btyZxChengbenId);
            ajax.start();
        };
        Feng.confirm("是否取消?", operation);
    }

    //dakuan_queren
});
